<?
// view utility
// 存储和视图相关的函数, functions related view
function yell($str, $loc='./'){
	setcookie('message',($str),time()+60);
	echo '<script>location="'.$loc.'"</script>';
	exit;
}
function cry($str, $loc='./'){
	setcookie('warning',($str),time()+60);
	echo '<script>location="'.$loc.'"</script>';
	exit;
}
// function yell($str, $loc=''){
// 	if($loc!='')$loc=';location="'.$loc.'"';
// 	echo '<script>alert("'.$str.'")'.$loc.'</script>';
// 	exit;
// }
function wheel($url){ echo "<script>window.location='".$url."'</script>"; exit; }
// handel error
function handle_error($errors){
	if(count($errors)){
		$tem=$errors[0];
		switch($tem['type']){
			case 'alert': cry($tem['message'],$tem['url']); break;
			case 'message': yell($tem['message'],$tem['url']); break;
			case 'nothing': wheel($tem['url']); break;
		}
	}
}
function echo_page_select($page, $page_count, $url)//page bar,  davee, 2011/9/22
{
$ptxt='';
$nid = 5;
if($page < 6) $nid= $nid*2-$page;
$min = $page - $nid;
$max = $page + $nid;
if ($max >= $page_count) { $max = $page_count; }
if ($min <= 1) { $min = 1; }
$ptxt.='<div id="mypage">';
//$ptxt.="<a href=\"".$url."1.html\">首 页</a>";
if ($page > 1)$ptxt.="<a class='l' href=\"".$url.($page-1).".html\">&lt;上一页</a>";
for ($i = $min; $i <= $max; $i++)
{
    if ($page != $i) $ptxt.="<a href=\"".$url.$i.".html\">".$i."</a>";
    else if ($page_count>1)$ptxt.="<span class=\"current\">".$i."</span>";
}
//$ptxt.="<a href=\"".$url.$page_count.".html\">尾 页</a>";
if($max>$page)$ptxt.="<a class='l' href=\"".$url.($page+1).".html\">下一页&gt;</a>";
$ptxt.=' &nbsp; <span class="pages">共'.$page_count.'页</span> &nbsp; &nbsp; <input name="page" id="page" class="i" value="'.$page.'" /> <a class="g" onClick="goto(\''.$url.'\'+$(\'#page\').val()+\'.html\');">跳转</a></div>';
return $ptxt;
}
function echo_page($page, $page_count, $url, $langid=2)//page bar,  davee, 2011/9/22
{
$ptxt='';
$nid = 5;
if($page < 6) $nid= $nid*2-$page;
$min = $page - $nid;
$max = $page + $nid;
if ($max >= $page_count) { $max = $page_count; }
if ($min <= 1){ $min = 1; }
$first='首页'; $last='尾页'; $prev='上一页'; $next='下一页';
switch($langid){
	case 3: $first='首頁'; $last='末頁'; $prev='上一頁'; $next='下一頁'; break;
	case 1:
	case 4: $first='First'; $last='Last'; $prev='Prev'; $next='Next'; break;
}
$ptxt.='<div id="mypage">';
//$ptxt.="<a href=\"".$url."1.html\">首 页</a>";
// if ($page_count > 1 && $page >1 ){
// 	$ptxt.="<a class='l' href=\"".$url."1.html\">&lt;&lt; ".$first."</a>";
// }
if ($page > 1) $ptxt.="<a class='l' href=\"".$url.($page-1).".html\">&lt; ".$prev."</a>";
for ($i = $min; $i <= $max; $i++)
{
    if ($page != $i) $ptxt.="<a href=\"".$url.$i.".html\">".$i."</a>";
    else if ($page_count>1)$ptxt.="<span class=\"current\">".$i."</span>";
}
//$ptxt.="<a href=\"".$url.$page_count.".html\">尾 页</a>";
if($max>$page)$ptxt.="<a class='l' href=\"".$url.($page+1).".html\">".$next." &gt;</a>";
// if ($page_count > 1 && $page < $page_count){
// 	$ptxt.="<a class='l' href=\"".$url.$page_count.".html\">".$last." &gt;&gt;</a>";
// }
if($langid==2)$ptxt.="<span style=\"padding:5px 15px;\" class=''>共&nbsp;".$page_count."&nbsp;页</span>";
$ptxt.="</div>";
return $ptxt;
}
function echo_page_raw($page, $page_count, $url, $langid=2)//page bar,  davee, 2011/9/22
{
$ptxt='';
$nid = 5;
if($page < 6) $nid= $nid*2-$page;
$min = $page - $nid;
$max = $page + $nid;
if ($max >= $page_count) { $max = $page_count; }
if ($min <= 1){ $min = 1; }
$first='首页'; $last='尾页'; $prev='上一页'; $next='下一页';
switch($langid){
	case 3: $first='首頁'; $last='末頁'; $prev='上一頁'; $next='下一頁'; break;
	case 1:
	case 4: $first='First'; $last='Last'; $prev='Prev'; $next='Next'; break;
}
$ptxt.='<div id="mypage">';
//$ptxt.="<a href=\"".$url."1.html\">首 页</a>";
// if ($page_count > 1 && $page >1 ){
// 	$ptxt.="<a class='l' href=\"".$url."1.html\">&lt;&lt; ".$first."</a>";
// }
if ($page > 1) $ptxt.="<a class='l' href=\"".$url.($page-1)."\">&lt; ".$prev."</a>";
for ($i = $min; $i <= $max; $i++)
{
    if ($page != $i) $ptxt.="<a href=\"".$url.$i."\">".$i."</a>";
    else if ($page_count>1)$ptxt.="<span class=\"current\">".$i."</span>";
}
//$ptxt.="<a href=\"".$url.$page_count.".html\">尾 页</a>";
if($max>$page)$ptxt.="<a class='l' href=\"".$url.($page+1)."\">".$next." &gt;</a>";
// if ($page_count > 1 && $page < $page_count){
// 	$ptxt.="<a class='l' href=\"".$url.$page_count.".html\">".$last." &gt;&gt;</a>";
// }
if($langid==2)$ptxt.="<span style=\"padding:5px 15px;\" class=''>共&nbsp;".$page_count."&nbsp;页</span>";
$ptxt.="</div>";
return $ptxt;
}

function echo_page_ajax($page, $page_count, $url, $langid=2)//page bar,  davee, 2011/9/22
{
$ptxt='';
$nid = 5;
if($page < 6) $nid= $nid*2-$page;
$min = $page - $nid;
$max = $page + $nid;
if ($max >= $page_count) { $max = $page_count; }
if ($min <= 1){ $min = 1; }
$first='首页'; $last='尾页'; $prev='上一页'; $next='下一页';
switch($langid){
	case 3: $first='首頁'; $last='末頁'; $prev='上一頁'; $next='下一頁'; break;
	case 1:
	case 4: $first='First'; $last='Last'; $prev='Prev'; $next='Next'; break;
}
$ptxt.='<div id="mypage">';

if ($page > 1) $ptxt.="<a class='l' href=\"javascript:".$url."(".($page-1).")\">&lt; ".$prev."</a>";
for ($i = $min; $i <= $max; $i++)
{
    if ($page != $i) $ptxt.="<a href=\"javascript:".$url."(".$i.")\">".$i."</a>";
    else if ($page_count>1)$ptxt.="<span class=\"current\">".$i."</span>";
}

if($max>$page)$ptxt.="<a class='l' href=\"javascript:".$url."(".($page+1).")\">".$next." &gt;</a>";

if($langid==2)$ptxt.="<span style=\"padding:5px 15px;\" class=''>共&nbsp;".$page_count."&nbsp;页</span>";
$ptxt.="</div>";
return $ptxt;
}

// 无限级分类遍历 unlimited level category
function unlimitedlevel($cate,$html_prefix='- - ',$parent_id = 0, $level = 0){
$arr = array();
foreach($cate as $v){
	if($v['parent_category_id']==$parent_id){
		$v['level'] = $level +1;
		$v['html_prefix'] = str_repeat($html_prefix,$level);
		$arr[] = $v;
		$arr = array_merge($arr,unlimitedlevel($cate,$html_prefix,$v['id'],$level+1));
	}
}
return $arr;
}
//父ID返回子ID, 一层
function get_level_children($cate,$pid){
$arr = array();
foreach($cate as $v){
	if($v['parent_category_id']==$pid){
		$arr[$v['id']] = $v;
		//$arr = array_merge($arr,get_level_children($cate,$v['id']));
	}
}
return $arr;
}

//子ID返回父ID, 从低到高
function get_level_parents($cate,$id){
$arr = array();
foreach($cate as $v){
	if($v['id']==$id){
		$arr[$id] = $v;
		$arr = array_merge($arr,get_level_parents($cate,$v['parent_category_id']));
	}
}
return $arr;
}

// final price, is_val=是否为子产品, get_discount=只返回折扣值
function price($item,$is_var=0, $get_discount=0){
	global $tree_product_category,$customer_grade,$currency_rate,$arr_grade, $arr_event,$product;
	$goon=1;
	$discount_type=1;
	$discount_value=0;
	$price=$item['price'];
	if($is_var){ // 子产品继承主产品的折扣
		$item=$product->get_row($item['product_id']);
	}
	$cateid=$item['category_id'];
	// 产品自有折扣 优先级最高 1
	$discount_item_event=$item['promotion_id'];
	if(in_array($discount_item_event,$arr_event)){
		$discount_type=$item['discount_type'];
		$discount_value=$item['discount_value'];
		$goon=0;
	}
	if($goon){
		// 分类折扣 优先级第二 2
		$cate_parents=get_level_parents($tree_product_category,$cateid);
		//pre($cate_parents);
		foreach($cate_parents as $val){
			if(in_array($val['promotion_id'],$arr_event)){
				$discount_type=$val['discount_type'];
				$discount_value=$val['discount_value'];
				$goon=0;
				break;
			}
		}
	}
	if($goon){
		// 用户级别折扣 优先级最低 3
		$discount_type=1;
		$discount_value=$arr_grade[$customer_grade]['discount'];
	}
	// 获取折扣
	if($get_discount) return $discount_value;
	// 计算价格
	if($discount_type==1){
		$price=round($price*$currency_rate*(1-$discount_value/100),2);
	}
	else $price=round(($price-$discount_value)*$currency_rate,2);
	return $price;
}
// original price
function discount($item,$is_var=0){
	global $currency_rate;
	$price=$item['price'];
	$discount=price($item,$is_var,1);
	if($discount==0) return '';
	return ' <span class="dis">&nbsp;'.round($price*$currency_rate,2).'&nbsp;</span>';
}
// order to html
function order2html($ref_no){
	global $sales_order,$sales_order_detail,$product,$product_variation,$arrcurrency,$baseurl,$lang,$option_label,$option_value;
	$order=$sales_order->get_row($ref_no,'ref_no');
	$items=$sales_order_detail->get_rows(1024,'sales_order_id',$order['id']);
	
	// $arr_product
	$arr_product=$in_array_product=array();
	foreach($items as $val) $in_array_product[]=$val['product_id'];
	$where=array();
	$where[]=array('name'=>'id','val'=>$in_array_product,'opn'=>'in');
	$tem=$product->get_rows_advanced(1024,$where);
	foreach($tem as $val) $arr_product[$val['id']]=$val;

	// currency
	$order_currency_sign=$arrcurrency[$order['currency_id']]['sign'];
	$order_currency_code=$arrcurrency[$order['currency_id']]['code'];

	// html
	$str_order='
	<style>
	.ordert { line-height:1.2; font-size:12px; }
	.ordert td,.ordert th { padding: 4px 6px; border: 1px solid #888;border-collapse:collapse; }
	</style>
	<div style="text-align:center; width:660px; padding: 12px 0 0 0;">
	<table cellpadding="0" cellspacing="0" width="660" class="ordert" style="border-collapse:collapse;">
	<thead>
		<tr>
		<th style="border: 1px solid #888;">'.WEB_ITEM.'</th>
		<th style="border: 1px solid #888;">'.WEB_UNIT_PRICE.'</th>
		<th style="border: 1px solid #888;">'.WEB_PURCHASE_QUANTITY.'</th>
		</tr>
	</thead>
	<tbody>';

	$total_qty=0;
	foreach ($items as $key => $val){
		$total_qty+=$val['quantity'];
		$tem_product=$arr_product[$val['product_id']];
		$tem_price=$val['unit_price'];
		$tem_pic='/upload/'.$tem_product['small_pic'];
		$tem_link=$baseurl.'product-'.$tem_product['id'].'.html';
		$tem_name=str_cut($tem_product['name_'.$lang],PRODUCT_TITLE_DISPLAY_LENGTH,$lang);

		// variation
		$str_var='';
		if($val['product_variation_id']){
			$tem_var=$product_variation->get_row($val['product_variation_id']);
			if($tem_var['size']) $str_var.=$option_label[2].': '.$option_value[$tem_var['size']].' &nbsp; ';
			if($tem_var['color']) $str_var.=$option_label[1].': '.$option_value[$tem_var['color']].' &nbsp; ';
			if($tem_var['other1']) $str_var.=$option_label[3].': '.$option_value[$tem_var['other1']].' &nbsp; ';
			if($tem_var['other2']) $str_var.=$option_label[4].': '.$option_value[$tem_var['other2']].' &nbsp; ';
		}

		$str_order.='<tr>
			<td style="border: 1px solid #888; width:300px; text-align: left;"><div><a style="float:left;" href="'.$tem_link.'" target="_blank"><img src="'.$tem_pic.'"  width="60" style="padding: 0 8px 0 0;"  /></a><div><a href="'.$tem_link.'" target="_blank">'.$tem_name.'</a><br /><span style="color:#888">'.$str_var.'</span></div><div class="clear"></div></div></td>
			<td style="border: 1px solid #888;">'.$order_currency_code.' '.$tem_price.'</td>
			<td style="border: 1px solid #888;">'.$val['quantity'].'</td>
			</tr>';
	}
	$str_order.='<tbody></table><div style="text-align:right">
	<p style="font-size:12px;">'.WEB_TOTAL_QUANTITY.': '.$total_qty.'<br />
	'.WEB_TOTAL_FOR_ITEMS.': '.$order_currency_code.' '.$order['order_price'].'<br />
	'.WEB_SHIPMENT_FEE.': '.$order_currency_code.' '.$order['shipment_cost'].'<br />';
	if($order['coupon_discount_amount']>0) $str_order.=WEB_COUPON_AMOUNT.': '.$order_currency_code.' '.$order['coupon_discount_amount'].'<br />';
	if($order['point_discount_amount']>0) $str_order.=WEB_POINT_AMOUNT.': '.$order_currency_code.' '.$order['point_discount_amount'].'<br />';
	$str_order.=WEB_SHOULD_PAY.': '.$order_currency_code.' '.$order['total_price'].'</p></div></div>';
	return $str_order;
}
// End of file view_utility.php